/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.model.item.action;

import static l1j.server.server.model.skill.L1SkillId.AWAKEN_ANTHARAS;
import static l1j.server.server.model.skill.L1SkillId.AWAKEN_FAFURION;
import static l1j.server.server.model.skill.L1SkillId.AWAKEN_VALAKAS;
import static l1j.server.server.model.skill.L1SkillId.SHAPE_CHANGE;
import l1j.server.server.datatables.PolyTable;
import l1j.server.server.datatables.SkillsTable;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1PolyMorph;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_ShowPolyList;
import l1j.server.server.templates.L1Skills;
import l1j.server.server.utils.Random;

/**
 * 處理變身物品
 * 變身卷軸、鱗片、藥水
 */
public class Poly {
	public static boolean usePolyScroll(L1PcInstance pc, int item_id, String s) {
		int awakeSkillId = pc.getAwakeSkillId();
		if ((awakeSkillId == AWAKEN_ANTHARAS) || (awakeSkillId == AWAKEN_FAFURION) || (awakeSkillId == AWAKEN_VALAKAS)) {
			pc.sendPackets(new S_ServerMessage(1384)); // 1384: 目前狀態中無法變身。
			return false;
		}

		int time = 0;
		if ((item_id == 40088) || (item_id == 40096)) { // 變形卷軸、象牙塔變形卷軸
			time = 1800;
		} else if (item_id == 49308) { // 福利變形藥水
			time = Random.nextInt(2401, 4800);
		} else if (item_id == 140088) { // 受祝福的 變形卷軸
			time = 2100;
		}

		L1PolyMorph poly = PolyTable.getInstance().getTemplate(s);
		if ((poly != null) || s.equals("")) {
			if (s.equals("")) {
				if ((pc.getTempCharGfx() == 6034) || (pc.getTempCharGfx() == 6035)) {
					return true;
				} else {
					pc.removeSkillEffect(SHAPE_CHANGE);
					return true;
				}
			} else if ((poly.getMinLevel() <= pc.getLevel()) || pc.isGm()) {
				L1PolyMorph.doPoly(pc, poly.getPolyId(), time, L1PolyMorph.MORPH_BY_ITEMMAGIC);
				return true;
			}
		}
		return false;
	}

	public static void usePolyScale(L1PcInstance pc, int itemId) {
		int time = 900;
		int awakeSkillId = pc.getAwakeSkillId();
		if ((awakeSkillId == AWAKEN_ANTHARAS) || (awakeSkillId == AWAKEN_FAFURION) || (awakeSkillId == AWAKEN_VALAKAS)) {
			pc.sendPackets(new S_ServerMessage(1384)); // 1384: 目前狀態中無法變身。
			return;
		}

		int polyId = 0;
		if (itemId == 41154) { // 暗之鱗
			polyId = 3101;
		} else if (itemId == 41155) { // 火之鱗
			polyId = 3126;
		} else if (itemId == 41156) { // 叛之鱗
			polyId = 3888;
		} else if (itemId == 41157) { // 恨之鱗
			polyId = 3784;
		} else if (itemId == 49220) { // 妖魔密使變形卷軸
			polyId = 6984;
			time = 1200;
		}
		L1PolyMorph.doPoly(pc, polyId, time, L1PolyMorph.MORPH_BY_ITEMMAGIC);
	}

	public static void usePolyPotion(L1PcInstance pc, int itemId) {
		int time = 1800;
		int awakeSkillId = pc.getAwakeSkillId();
		if ((awakeSkillId == AWAKEN_ANTHARAS) || (awakeSkillId == AWAKEN_FAFURION) || (awakeSkillId == AWAKEN_VALAKAS)) {
			pc.sendPackets(new S_ServerMessage(1384)); // 1384: 目前狀態中無法變身。
			return;
		}

		int polyId = 0;
		if (itemId == 41143) { // 海賊骷髏首領變身藥水
			polyId = 6086;
			time = 900;
		} else if (itemId == 41144) { // 海賊骷髏士兵變身藥水
			polyId = 6087;
			time = 900;
		} else if (itemId == 41145) { // 海賊骷髏刀手變身藥水
			polyId = 6088;
			time = 900;
// 夏納的變身卷軸(等級30)
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6822;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6823;
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6824;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6825;
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6826;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6827;
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6828;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6829;
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6830;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6831;
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7139;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7140;
		} else if ((itemId == 49149) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7141;
		} else if ((itemId == 49149) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7142;
// 夏納的變身卷軸(等級40)
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6832;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6833;
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6834;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6835;
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6836;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6837;
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6838;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6839;
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6840;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6841;
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7143;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7144;
		} else if ((itemId == 49150) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7145;
		} else if ((itemId == 49150) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7146;
// 夏納的變身卷軸(等級52)
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6842;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6843;
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6844;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6845;
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6846;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6847;
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6848;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6849;
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6850;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6851;
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7147;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7148;
		} else if ((itemId == 49151) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7149;
		} else if ((itemId == 49151) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7150;
// 夏納的變身卷軸(等級55)
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6852;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6853;
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6854;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6855;
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6856;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6857;
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6858;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6859;
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6860;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6861;
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7151;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7152;
		} else if ((itemId == 49152) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7153;
		} else if ((itemId == 49152) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7154;
// 夏納的變身卷軸(等級60)
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6862;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6863;
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6864;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6865;
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6866;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6867;
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6868;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6869;
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6870;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6871;
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7155;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7156;
		} else if ((itemId == 49153) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7157;
		} else if ((itemId == 49153) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7158;
// 夏納的變身卷軸(等級65)
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6872;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6873;
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6874;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6875;
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6876;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6877;
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6878;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6879;
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6880;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6881;
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7159;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7160;
		} else if ((itemId == 49154) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7161;
		} else if ((itemId == 49154) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7162;
// 夏納的變身卷軸(等級70)
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isCrown()) {
			polyId = 6882;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isCrown()) {
			polyId = 6883;
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isKnight()) {
			polyId = 6884;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isKnight()) {
			polyId = 6885;
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isElf()) {
			polyId = 6886;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isElf()) {
			polyId = 6887;
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isWizard()) {
			polyId = 6888;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isWizard()) {
			polyId = 6889;
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isDarkelf()) {
			polyId = 6890;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isDarkelf()) {
			polyId = 6891;
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isDragonKnight()) {
			polyId = 7163;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isDragonKnight()) {
			polyId = 7164;
		} else if ((itemId == 49155) && (pc.get_sex() == 0) && pc.isIllusionist()) {
			polyId = 7165;
		} else if ((itemId == 49155) && (pc.get_sex() == 1) && pc.isIllusionist()) {
			polyId = 7166;
		} else if ((itemId == 49139)) { // 起司蛋糕
			polyId = 6137; // 52級死亡騎士
			time = 900;
		}
		L1PolyMorph.doPoly(pc, polyId, time, L1PolyMorph.MORPH_BY_ITEMMAGIC);
	}

	public static void polyAction(L1PcInstance attacker, L1Character cha) {
		boolean isSameClan = false;
		if (cha instanceof L1PcInstance) {
			L1PcInstance pc = (L1PcInstance) cha;
			if ((pc.getClanid() != 0) && (attacker.getClanid() == pc.getClanid())) { // 目標為盟友
				isSameClan = true;
			}
		}
		if ((attacker.getId() != cha.getId()) && !isSameClan) { // 非自身及盟友
			int probability = 3 * (attacker.getLevel() - cha.getLevel()) + 100 - cha.getMr();
			int rnd = Random.nextInt(100) + 1;
			if (rnd > probability) {
				attacker.sendPackets(new S_ServerMessage(79));
				return;
			}
		}

		int[] polyArray = {
				29, 945, 947, 979, 1037, 1039, 3860, 3861, 3862, 3863, 3864,
				3865, 3904, 3906, 95, 146, 2374, 2376, 2377, 2378, 3866, 3867,
				3868, 3869, 3870, 3871, 3872, 3873, 3874, 3875, 3876
		};

		int pid = Random.nextInt(polyArray.length);
		int polyId = polyArray[pid];

		if (cha instanceof L1PcInstance) {
			L1PcInstance pc = (L1PcInstance) cha;
			int awakeSkillId = pc.getAwakeSkillId();
			if ((awakeSkillId == AWAKEN_ANTHARAS) || (awakeSkillId == AWAKEN_FAFURION) || (awakeSkillId == AWAKEN_VALAKAS)) {
				if (attacker.getId() == pc.getId()) {
					attacker.sendPackets(new S_ServerMessage(1384)); // 目前狀態中無法變身。
				} else {
					attacker.sendPackets(new S_ServerMessage(79));
				}
				return;
			}

			if (pc.getInventory().checkEquipped(20281)) { // 裝備變形控制戒指
				pc.sendPackets(new S_ShowPolyList(pc.getId()));
				if (!pc.isShapeChange()) {
					pc.setShapeChange(true);
				}
			} else {
				if (attacker.getId() != pc.getId()) {
					pc.sendPackets(new S_ServerMessage(241, attacker.getName())); // %0%s 把你變身。
				}
				L1Skills skillTemp = SkillsTable.getInstance().getTemplate(SHAPE_CHANGE);
				L1PolyMorph.doPoly(pc, polyId, skillTemp.getBuffDuration(), L1PolyMorph.MORPH_BY_ITEMMAGIC, false);
			}
		} else if (cha instanceof L1MonsterInstance) {
			L1MonsterInstance mob = (L1MonsterInstance) cha;
			if (mob.getLevel() < 50) {
				int npcId = mob.getNpcTemplate().get_npcId();
				if ((npcId != 45338) && (npcId != 45370) && (npcId != 45456 // クロコダイル、バンディットボス、ネクロマンサー
						) && (npcId != 45464) && (npcId != 45473) && (npcId != 45488 // セマ、バルタザール、カスパー
						) && (npcId != 45497) && (npcId != 45516) && (npcId != 45529 // メルキオール、イフリート、ドレイク(DV)
						) && (npcId != 45458)) { // ドレイク(船長)
					L1Skills skillTemp = SkillsTable.getInstance().getTemplate(SHAPE_CHANGE);
					L1PolyMorph.doPoly(mob, polyId, skillTemp.getBuffDuration(), L1PolyMorph.MORPH_BY_ITEMMAGIC);
				}
			}
		}
	}
}